package com.bjpowernode.seckillweb.service.impl;

import com.bjpowernode.constant.Constants;
import com.bjpowernode.entity.Goods;
import com.bjpowernode.seckillweb.service.GoodsService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<Goods> queryGoosList() {
        List<Goods> goodsList = new ArrayList<>();
        Set<String> keys = redisTemplate.keys(Constants.REDIS_GOODS + "*");
        keys.forEach(key -> {
            Goods goods = (Goods) redisTemplate.opsForValue().get(key);
            goodsList.add(goods);
        });
        return goodsList;
    }

    @Override
    public Goods getById(Integer id) {
        return (Goods) redisTemplate.opsForValue().get(Constants.REDIS_GOODS + id);
    }
}